package tyk.common;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/*
* Handler拦截器
* 用于拦截用户未登录访问后台管理
* */
@Component
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if (request.getRequestURI().equals("/")) return true;  // 放行首页

        HttpSession httpSession = request.getSession();
        if (httpSession.getAttribute("username") != null) return true;

        else response.sendRedirect("/admin/login"); //未登录，则重定向到登录页
        return false;
    }
}
